package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }

    /*
        http://userprofile.gmall.com    /user-group
     */
    @PostMapping(value = "/user-group")
    public Object handleUserGroup(@RequestBody UserGroup userGroup){

        //①先写入mysql,获取自增id
        userGroupService.writeUserGroupToMysql(userGroup);

        //②计算人群包人数,需要把人群存储到ck的user_group中，需要UserGroup的id，id只会在写入mysql生成
        Long userGroupNum = userGroupService.calulateUserGroupNum(userGroup);

        userGroup.setUserGroupNum(userGroupNum);

        //③将人群包同步到redis一份
        userGroupService.writeDataToRedis(userGroup );

        //更新Mysql，更新人数
        userGroupService.saveOrUpdate(userGroup);

        return "sucess";

    }

    //user-group-evaluate
    @PostMapping(value = "/user-group-evaluate")
    public Object evaluateUserGroupNum(@RequestBody UserGroup userGroup){

        //发送sql，查询ck中用户分群信息
        //calulateUserGroupNum查询的是已经写入的人群包
        //预估:  未写入人群包
        Long userGroupNum = userGroupService.evaluateUserGroupNum(userGroup);

        return userGroupNum;

    }

    /*
            /user-group-refresh/6?busiDate=2020-06-15

            更新:
                    一个人群包有数据，和元数据
                        数据:
                                正式存储:  ck 的 user_group
                                缓存:     redis

                        元数据: mysql
     */
    @PostMapping(value = "/user-group-refresh/{id}")
    public Object updateUserGroup(String busiDate,@PathVariable("id") String id){

        //根据Id查询出人群包的元数据。 从元数据知道计算的条件
        UserGroup userGroup = userGroupService.getById(id);
        //把tagConditions赋值
        List<TagCondition> tagConditions = JSON.parseArray(userGroup.getConditionJsonStr(), TagCondition.class);
        userGroup.setTagConditions(tagConditions);

        //业务日期
        userGroup.setBusiDate(busiDate);

        //重新计算人群包，更新ck
        userGroupService.updateClickhouse(userGroup);

       //更新redis
        Long num = userGroupService.writeDataToRedis(userGroup);

        //更新mysql
        //获取到当天最新的人数
        userGroup.setUserGroupNum(num);

        //添加更新日期
        userGroup.setUpdateTime(new Date());

        return "success";

    }





}

